package com.example.permissionsystem1_1.businessFunction.service.interfaces.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.permissionsystem1_1.businessFunction.dao.entity.po.TestPo;
import com.example.permissionsystem1_1.businessFunction.dao.repository.TestDao;
import com.example.permissionsystem1_1.businessFunction.service.converter.TestServiceConverter;
import com.example.permissionsystem1_1.businessFunction.service.entity.reqModel.testReqModel.*;
import com.example.permissionsystem1_1.businessFunction.service.entity.rspModel.testRspModel.*;
import com.example.permissionsystem1_1.businessFunction.service.interfaces.TestService;
import com.example.permissionsystem1_1.common.businessFunction.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TestServiceImpl extends BaseServiceImpl implements TestService {
    @Autowired
    private TestDao testDao;
    @Override
    public TestInsertRspModel insert(TestInsertReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        Integer resultInt=-1;
        if(po!=null){
            resultInt=testDao.insert(po);
        }
        logger.error("测试日志打印");
        return TestServiceConverter.covertToRspModel(resultInt);
    }

    @Override
    public TestInsertBatchRspModel insertBatch(TestInsertBatchReqModel reqModel) {
        List<TestPo> pos=TestServiceConverter.convertReqModelToPo(reqModel);
        Integer resultSum=-1;
        if(pos!=null&&pos.size()>0){
            resultSum=testDao.insertBatch(pos);
        }
        return TestServiceConverter.covertToInsertBatchRspModel(resultSum);
    }

    @Override
    public TestDeleteByIdRspModel deleteById(TestDeleteByIdReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        return TestServiceConverter.covertToDeleteByIdRspModel(testDao.deleteById(po.getId()));
    }

    @Override
    public TestDeletesRspModel deletes(TestDeletesReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        Integer resultSum=-1;
        if(po!=null){
            resultSum=testDao.deletes(po);
        }
        return TestServiceConverter.covertToDeletesRspModel(resultSum);
    }

    @Override
    public TestDeleteBatchByIdsRspModel deleteBatchByIds(TestDeleteBatchByIdsReqModel reqModel) {
        TestPo po=TestServiceConverter.convertReqModelToPo(reqModel);
        Integer resultSum=-1;
        if(po!=null&&po.getIds()!=null&&po.getIds().size()>0){
            resultSum=testDao.deleteBatchByIds(po.getIds());
        }
        return TestServiceConverter.covertToDeleteBatchByIdsRspModel(resultSum);
    }

    @Override
    public TestUpdateByIdRspModel updateById(TestUpdateByIdReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        Integer resultInt=-1;
        if(po!=null){
            resultInt=testDao.updateById(po);
        }
        return TestServiceConverter.covertToUpdateByIdRspModel(resultInt);
    }

    @Override
    public TestUpdatesRspModel updates(TestUpdatesReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        Integer resultSum=-1;
        if(po!=null&&po.getByPo()!=null&&po.getSetPo()!=null){
            resultSum=testDao.updates(po.getByPo(),po.getSetPo());
        }
        return TestServiceConverter.covertToUpdatesRspModel(resultSum);
    }

    @Override
    public TestUpdateBatchByIdsRspModel updateBatchByIds(TestUpdateBatchByIdsReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        Integer resultSum=-1;
        if(po!=null&&po.getIds()!=null&&po.getIds().size()>0){
            resultSum=testDao.updateBatchByIds(po);
        }
        return TestServiceConverter.covertToUpdateBatchByIdsRspModel(resultSum);
    }

    @Override
    public TestSelectByIdRspModel selectById(TestSelectByIdReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        if(po!=null&&po.getId()!=null){
            po=testDao.selectById(po.getId());
        }
        return TestServiceConverter.covertToSelectByIdRspModel(po);
    }

    @Override
    public TestSelectsRspModel selects(TestSelectsReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        List<TestPo> pos=new ArrayList<>();
        if(po!=null){
            pos=testDao.selects(po);
        }
        return TestServiceConverter.covertToSelectsModel(pos);
    }

    @Override
    public TestSelectBatchByIdsRspModel selectBatchByIds(TestSelectBatchByIdsReqModel reqModel) {
        TestPo po= TestServiceConverter.convertReqModelToPo(reqModel);
        List<TestPo> pos=new ArrayList<>();
        if(po!=null){
            pos=testDao.selectBatchByIds(po.getIds());
        }
        return TestServiceConverter.covertToSelectBatchByIdsModel(pos);
    }

    @Override
    public TestSelectPageRspModel selectPage(TestSelectPageReqModel reqModel) {
        TestPo po=TestServiceConverter.convertReqModelToPo(reqModel);
        if(po==null){
            return null;
        }
        Page page=new Page(reqModel.getPageNo(),reqModel.getPageSize());
        //默认按照年龄排序
        page.addOrder(OrderItem.asc("id"));
        IPage<TestPo> iPagePo=testDao.selectPage(page,po);
        return TestServiceConverter.covertToSelectPageModel(iPagePo);
    }
}
